package filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Customer;
import service.CustomerService;
import service.LoginService;

@WebFilter("/*")
public class Filter2_LoginCheckFilter implements Filter {

	public Filter2_LoginCheckFilter() {
	}

	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String path = req.getServletPath();
		if (req.getSession().getAttribute("customer") == null && !path.substring(1).equals("login.html")
				&& !path.substring(1, 4).equals("css") && !path.substring(1, 6).equals("image")
				&& !path.substring(1, 3).equals("js") && !path.substring(1).equals("RegisterServlet") 
				&& !path.substring(1).equals("wechat.html") && !path.substring(1).equals("WechatLoginServlet")
				&& !path.substring(1).equals("LoginServlet") && !path.substring(1, 6).equals("admin")
				&& !path.contains("Servlet")) {
			if (req.getSession().getAttribute("customer") == null) {
				resp.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx759e6da18b169ee9&redirect_uri=http%3a%2f%2fjdmsr.xin%2fbang%2fWechatLoginServlet&response_type=code&scope=snsapi_base&state=123#wechat_redirect");
				return;
//				Customer customer = new CustomerService().getCustomerById("1").list.get(0);
//				req.getSession().setAttribute("customer", customer);
			}
			resp.sendRedirect(req.getContextPath() + "/index.html");
		}
		
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}

}
